package com.ch.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ch.model.bo.ReportBO;
import com.ch.model.entity.*;
import com.ch.model.query.PaibanQuery;
import com.ch.model.query.ReportQuery;
import com.ch.service.*;
import com.yy.common.result.PageResult;
import com.yy.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author YZJ
 * @Create 2024/6/14 9:45
 */
@RestController
@RequestMapping("/registration")
@Tag(name = "门诊管理API")
@Slf4j
public class RegisteredController {
	@Resource
	private RegisteredtypeService registeredtypeService;//    挂号类型

	@Resource
	private com.ch.service.DepartmentsService DepartmentsService;//    科室

	@Resource
	private ReportService reportService;// 挂号数据库

	@Resource
	private CashierService cashierService;// 门诊收费

	@Resource
	private DoctorService doctorService;//获取医生

	@Resource
	private PaibanService paibanService;//获取医生

	@Resource
	private PharmacyService pharmacyService;//获得药房信息

	@Resource
	private OutpatienttypeService outpatienttypeService;//获得药房信息


	@GetMapping("/getRType")
	@Operation(summary = "查询挂号类型")
	public Result getRType() {
		return Result.success(registeredtypeService.list());
	}

	@GetMapping("/getDepartments")
	@Operation(summary = "查询科室类型")
	public Result getDepartments() {
		return Result.success(DepartmentsService.list());
	}

	@GetMapping("/getReportList")
	@Operation(summary = "分页查询患者库")
	public PageResult<ReportBO> Page(ReportQuery reportQuery) {
		IPage<ReportBO> page = reportService.getPage(reportQuery);
		return PageResult.success(page);
	}

	@GetMapping("/getCashier")
	@Operation(summary = "门诊收费")
	public PageResult<Cashier> getCashierPage(int pageNum, int pageSize, int id) {
		QueryWrapper<Cashier> wrapper = new QueryWrapper<>();
		wrapper.eq("reportId", id);
		Page<Cashier> page_ = new Page<Cashier>(pageNum, pageSize);
		return PageResult.success(cashierService.page(page_, wrapper));
	}

	@GetMapping("/getDoctor")
	@Operation(summary = "根据挂号类型，科室获得当前在岗医生")
	public Result getDoctor(Integer typeId, Integer depId, String date) {
		return Result.success(doctorService.getDoctor(typeId, depId, date));
	}

	@PostMapping("/addReport")
	@Operation(summary = "添加挂号病人")
	public Result addReport(@RequestBody Report report) {
		report.setState(1);
		reportService.save(report);
		return Result.success();
	}


	@GetMapping("/getPaiban1")
	@Operation(summary = "查询排班")
	public Result getPaiban1() {
		List<Paiban> list = paibanService.getList();
		System.out.println(list);
		return Result.success(list);
	}

	@GetMapping("/getReport1")
	@Operation(summary = "查询当天挂号的病人信息")
	public Result getReport1() {
		String date = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
		List<ReportBO> report = reportService.getReport(date);
		for (ReportBO reportBO : report) {
			StringBuffer sb = new StringBuffer(reportBO.getCarid());
			sb.replace(4, 14, "**********");
			reportBO.setCarid(sb.toString());
		}
		return Result.success(report);
	}

	@DeleteMapping("/deleteR")
	@Operation(summary = "删除挂号病人")
	public Result deleteR(@RequestBody Integer reportId) {
		Integer i = reportService.deleteR(reportId);
		if (i == 0) return Result.failed();
		else return Result.success();
	}

	@PutMapping("/updatedR/{reportId}/{diagnose}")
	@Operation(summary = "转住院")
	public Result updatedR(@PathVariable Integer reportId,@PathVariable String diagnose) {
		System.out.println("reportId = " + reportId + ", diagnose = " + diagnose);
		boolean b = reportService.updateR(reportId,diagnose);
		return Result.judge(b);
	}

	@GetMapping("/getPaiban")
	@Operation(summary = "分页查询排班")
	public PageResult<Paiban> getPaiban(PaibanQuery paibanQuery) {
		IPage<Paiban> page = paibanService.getPage(paibanQuery);
		return PageResult.success(page);
	}

	@GetMapping("/getPaibanById/{paiid}")
	@Operation(summary = "查询科室类型")
	public Result getPaibanById(@PathVariable Integer paiid) {
		LambdaQueryWrapper<Paiban> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Paiban::getPaiid, paiid);
		return Result.success(paibanService.getOne(wrapper));
	}

	@PutMapping("/updatedP")
	@Operation(summary = "修改排班")
	public Result updatedP(@RequestBody Paiban paiban) {
		boolean b = paibanService.updateById(paiban);
		return Result.judge(b);
	}

	@GetMapping("/getPharmacyU")
	@Operation(summary = "查询药房表")
	public Result getPharmacyU() {
		List<Pharmacy> list = pharmacyService.getList();
		return Result.success(list);
	}

	@PutMapping("/updatedRD/{reportId}")
	@Operation(summary = "填写病因")
	public Result updatedRD(@PathVariable Integer reportId, @RequestBody String diagnose) {
		Report report = new Report();
		report.setReportid(reportId);
		report.setDiagnose(diagnose);
		boolean b = reportService.updateById(report);
		return Result.judge(b);
	}

	@GetMapping("/getRID/{id}")
	@Operation(summary = "根据id查询患者病因")
	public Result getRID(@PathVariable Integer id) {
		QueryWrapper<Report> wrapper = new QueryWrapper<>();
		wrapper.eq("reportid", id);
		Report one = reportService.getOne(wrapper);
		return Result.success(one);
	}

	@GetMapping("/getC/{id}/{state}")
	@Operation(summary = "根据id查询患者药品")
	public Result getC(@PathVariable Integer id, @PathVariable Integer state) {
		QueryWrapper<Cashier> wrapper = new QueryWrapper<>();
		wrapper.eq("reportid", id);
		wrapper.eq("state", state);
		List<Cashier> list = cashierService.list(wrapper);
		return Result.success(list);
	}

	@DeleteMapping("/deleteCa/{id}/{durgnum}/{durgname}")
	@Transactional
	@Operation(summary = "删除患者药品")
	public Result deleteCa(@PathVariable Integer id, @PathVariable Integer durgnum, @PathVariable String durgname) {
		QueryWrapper<Pharmacy> wrapper = new QueryWrapper<>();
		wrapper.eq("pharmacyname", durgname);
		Pharmacy one = pharmacyService.getOne(wrapper);
		int i = durgnum + one.getDrugstorenum();
		Pharmacy pharmacy = new Pharmacy();
		pharmacy.setPharmacyid(one.getPharmacyid());
		pharmacy.setDrugstorenum(i);
		pharmacyService.updateById(pharmacy);
		boolean b = cashierService.removeById(id);
		return Result.success(b);
	}

	@PostMapping("/addCa")
	@Transactional
	@Operation(summary = "添加患者药品")
	public Result addCa(@RequestBody Cashier cashier) {
		Pharmacy pharmacy = new Pharmacy();
		pharmacy.setPharmacyid(cashier.getId());
		pharmacy.setDrugstorenum(cashier.getNum());
		pharmacyService.updateById(pharmacy);
		cashier.setState(1);
		cashierService.save(cashier);
		return Result.success();
	}


	@GetMapping("/getOutp")
	@Operation(summary = "查询检查项目")
	public Result getOutp() {
		List<Outpatienttype> list = outpatienttypeService.getlist();
		return Result.success(list);
	}

	@PostMapping("/addCa1")
	@Transactional
	@Operation(summary = "添加患者项目")
	public Result addCa1(@RequestBody Cashier cashier) {
		cashier.setState(2);
		cashierService.save(cashier);
		return Result.success();
	}

	@DeleteMapping("/deleteCa1/{id}")
	@Transactional
	@Operation(summary = "删除患者项目")
	public Result deleteCa1(@PathVariable Integer id) {
		boolean b = cashierService.removeById(id);
		return Result.success(b);
	}

	@Resource
	private PayService payService;

	@PostMapping("/addP/{state}")
	@Transactional
	@Operation(summary = "缴费")
	public Result addP(@RequestBody Pay pay, @PathVariable Integer state) {
		QueryWrapper<Cashier> wrapper = new QueryWrapper<>();
		wrapper.in("cashier", pay.getCids());
		Cashier cashier = new Cashier();
		cashier.setState(state);
		cashierService.update(cashier, wrapper);
		payService.save(pay);
		return Result.success();
	}

	@PostMapping("/jianCa")
	@Operation(summary = "添加项目检查")
	public Result jianCa(@RequestBody Cashier cashier) {
		cashierService.updateById(cashier);
		return Result.success();
	}

	@GetMapping("/getCj/{cashier}")
	@Operation(summary = "查询病人症状")
	public Result getCj(@PathVariable Integer cashier) {
		Cashier byId = cashierService.getById(cashier);
		return Result.success(byId);
	}
}
